import request from './request'

export interface ApiResponse<T = any> {
  code: number
  message: string
  data: T
}

export interface Dept {
  id: number
  deptName: string
  deptCode: string
  parentDeptCode?: string
  createdAt: string
  updatedAt: string
  isDeleted: boolean
  children?: Dept[]
}

export interface CreateDeptRequest {
  deptName: string
  deptCode?: string
  parentDeptCode?: string
}

export interface UpdateDeptRequest {
  id: number
  deptName: string
}

export interface DeptListParams {
  keyWord?: string
  pageDto: {
    pageNumber: number
    pageSize: number
  }
}

export interface PagedResult<T> {
  items: T[]
  totalCount: number
  pageNumber: number
  pageSize: number
}

export interface TreeNode {
  id: number
  label: string
  value: string
  children?: TreeNode[]
}

// 获取部门列表
export const getDeptList = (params: DeptListParams): Promise<ApiResponse<PagedResult<Dept>>> => {
  return request.post('/dept/getList', params)
}

// 获取部门树形结构
export const getDeptTree = (): Promise<ApiResponse<Dept[]>> => {
  return request.post('/dept/getTree', {})
}

// 创建部门
export const createDept = (data: CreateDeptRequest): Promise<any> => {
  return request.post('/dept/addDept', data)
}

// 更新部门
export const updateDept = (data: UpdateDeptRequest): Promise<any> => {
  return request.post('/dept/updateDept', data)
}

// 删除部门
export const deleteDept = (id: number): Promise<any> => {
  return request.post('/dept/delById', { id })
}
